import  redis
class RedisCon:
    def __init__(self):
        self.r = redis.Redis(host='localhost', port=6379,db=1)
    # 获取所有
    def list_getall(self,key,start,end):
        return self.r.exists(key,start,end)
    # 列表存
    def list_rpush(self,key,val):
        self.r.rpush(key,val)
    #存set
    def insert_set(self,key,val):
        self.r.sadd(key,val)
    #取set
    def find_set(self,key):
        val = self.r.smembers(key)
        if val:
            list =[]
            for i in val:
                list.append(str(i,encoding='utf8'))
            return list
        else:
            return None
    #hash类型添加
    def insert_hash(self,key,params,value):
        self.r.hset(key,params,value)
    #取某个属性的值
    def get_value_hash(self,key,params):
        return self.r.hget(key,params)
    #取所有属性的值
    def get_all_hash(self,key):
        return self.r.hgetall(key)
    #删除某个属性
    def del_hash(self,key,params):
        self.r.hdel(key,params)
    #字符串存取
    def set_str(self,key,value,time=0):
        if time > 0:
            self.r.setex(key,time,value)
        else:
            self.r.set(key,value)
    def get_str(self,key):
        return self.r.get(key)
    #列表存
    def list_push(self,key,val):
        self.r.lpush(key,val)
    #列表取
    def list_pop(self,key):
        return self.r.rpop(key)
    #获取长度
    def list_len(self,key):
        if self.r.exists(key):
            return self.r.llen(key)
        else:
            return 0
    # set （排行，退出专用）
# +++++++++++++++++++++++++++++++++++++++++++++++++
    def sortedset_add(self,pkey,key,score):
        mapping = {key:score}
        self.r.zadd(pkey,mapping)
    #获取
    def sortedset_getall(self,pkey):
        return self.r.zrange(pkey,0,-1)
    #根据分数的区间获取
    def sortedset_getbyscore(self,pkey,min,max):
        return self.r.zrangebyscore(pkey,min,max)
    #删除
    def sortedset_delete(self,pkey,key):
        self.r.zrem(pkey,key)
# +++++++++++++++++++++++++++++++++++++++++++++++++
    # 列表取
    def list_lpop(self, key):
        return self.r.lpop(key)

r = RedisCon()